const baseUrl = 'http://localhost:8080/api'
const request = (url = '', date = {}, type = 'GET', header = {
	"token": uni.getStorageSync("token")
}) => {
	return new Promise((resolve, reject) => {
		uni.request({
			method: type,
			url: baseUrl + url,
			data: date,
			header: header,
			dataType: 'json',
		}).then((response) => {
			let [error, res] = response;
			if (res.data.code == 0) {
				resolve(res.data.data);
			} else if (res.data.code == 10010){
				uni.clearStorage();
				uni.switchTab({
					url: '/pages/tabbar/user/user',
					success() {
						let page = getCurrentPages().pop();
					}
				})
				uni.showToast({
					title: '登录已过期',
					icon: 'error',
					duration: 2000
				});
			}
			else {
				uni.showToast({
					title: '后台错误啦',
					icon: 'error',
					duration: 2000
				});
			}
		}).catch(error => {
			let [err, res] = error;
			reject(err)
		})
	});
}
export default request
